<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog
      :close-on-click-modal="false"
      title="编辑等级"
      :modal-append-to-body="false"
      :visible.sync="visible"
      :show="show"
      :before-close="closeDialog"
      @close="$emit('update:show',false)"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="data" :rules="dataYZ" ref="data" class="mar_top" label-width="120px">
        <div
          style="display: flex;margin-bottom: 20px; margin-left: 20px;font-size: 14px;"
          v-if="is_upgrade == 1 && data.vip_time && data.vip_time > 0 && Number(data.vip_time) > Number(JSON.stringify(new Date().getTime()).substr(0,10))"
        >
          到期时间：
          <small>{{data.vip_time | getTimeFormat }}</small>
          <br />
        </div>

        <el-form-item label="会员等级" prop="rating">
          <el-col :span="12">
            <el-select v-model="data.rating" placeholder="请选择" style="width:100%">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="到期时间" v-if="is_upgrade == 1 " prop="rating">
          <el-col :span="12">
            <el-date-picker
              v-model="vip_time"
              type="datetime"
              value-format="timestamp"
              placeholder="选择到期时间"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item
          label="虚拟币等级"
          v-if="plugins.xnbkj && plugins.xnbkj.is_open == 1"
          prop="rating">
          <el-col :span="12">
            <el-select v-model="data.coin_rating" placeholder="请选择" style="width:100%">
              <el-option v-for="(item,i) in list" :key="i" :label="item.title" :value="item.id"></el-option>
            </el-select>
          </el-col>
        </el-form-item>

        <el-form-item label="是否锁定升级">
          <el-col :span="12">
            <el-radio v-model="data.lock_rating" label="0">是</el-radio>
            <el-radio v-model="data.lock_rating" label="1">否</el-radio>
          </el-col>
        </el-form-item>

        <div class="mar_bottom_15">
          <span class="sizeColor size_13 sp_style"></span>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="addUpdate('data')"
          >确定</el-button>
        </div>
      </el-form>
    </el-dialog>
  </div>
</template>


<style lang="less" scoped>
</style>

<script>
import { mapState } from "vuex";
export default {
  data() {
    return {
      is_upgrade: null,
      vip_time: "",
      options: [
        {
          value: "",
          label: ""
        }
      ],
      list: [],
      data: {
        id: "",
        rating: "",
        lock_rating: "1",
        coin_rating: ""
      },
      loading: false,
      dataYZ: {
        //     title:[
        //     { validator: checkTitle, trigger: 'blur' }
        //     ],
      },
      visible: this.show
    };
  },
  props: {
    show: Boolean,
    editId: String,
    clickNum: Number
  },
  computed: {
    ...mapState({
      plugins: state => state.plugin
    })
  },
  created() {},
  methods: {
    getupgrade() {
      //会员费管理  如果为1 取消编辑升级调剂
      this.$api.getSettings.findConfig({ iden: "vip_mode " }).then(res => {
        if (res.data.code == 200) {
          this.is_upgrade = res.data.result;
        }
      });
    },
    handChange(val) {
      console.log(val);
      this.data.rating = val;
    },
    getOptionList() {
      var data = {};
      this.$api.getUser.getRatingOption(data).then(res => {
        this.options = res.data.result;
      });
    },
    getXnbRatting() {
      this.$api.getCoin.getCoinGrade({}).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
      });
    },
    closeDialog() {
      this.visible = false;
    },
    getDetails(id) {
      this.getupgrade();
      var data = {};
      data["id"] = id;
      this.$api.getUser.userRatingEdit(data).then(res => {
        this.data = res.data.result;
        this.vip_time = this.$fnc.vuetime(this.data.vip_time);
      });
    },
    addUpdate(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          var params = {};
          params["id"] = this.data.id;
          params["rating"] = this.data.rating;
          params["lock_rating"] = this.data.lock_rating;
          params["coin_rating"] = this.data.coin_rating;
          params["vip_time"] = this.$fnc.phptime(this.vip_time);
          this.$api.getUser.userRatingSaveEdit(params).then(res => {
            if (res.data.code == 200 && res.data.result != null) {
              this.$emit("parent", "这里刷新父页面");
              this.$fnc.alertSuccess({ text: "编辑成功" }); //成功弹出框
              this.visible = false;
            } else {
              this.$fnc.alertError(res.data.result); //失败弹出框
            }
          });
        }
      });
    }
  },
  watch: {
    show() {
      console.log(this.show);
      if (this.show) {
        this.getXnbRatting();
      }
      this.visible = this.show;
    },
    clickNum(id) {
      this.getDetails(this.editId);
    }
  },
  mounted() {
    this.getOptionList();
  },
  created() {}
};
</script>
